package de.hsw.facbook.model.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import de.hsw.facbook.model.dao.DAOBase;
import de.hsw.facbook.model.dao.MessageDAO;
import de.hsw.facbook.model.entities.Message;
import de.hsw.facebook.model.rowMapper.MessageRowMapper;

public class MessageDAOImpl extends DAOBase implements MessageDAO {
	private final Log logger = LogFactory.getLog(MessageDAOImpl.class);
	private MessageRowMapper messageRowMapper = new MessageRowMapper();

	@Override
	public Message getByID(int ID) {
		logger.debug("entering getByID");

		Message result = new Message();

		String query = "Select messageID,accountID,Subject,Text,receiverID FROM Message WHERE messageID = ?";

		try {
			Connection con = openConnection();
			PreparedStatement prepStat = con.prepareStatement(query);
			prepStat.setInt(1, ID);
			ResultSet rs = prepStat.executeQuery();
			// Wir erwarten nur ein Ergebnis
			if (rs.next()) {
				result = this.messageRowMapper.mapRow(rs);
			}
			rs.close();
			prepStat.close();
			con.close();
		} catch (SQLException e) {
			logger.error("SQLException occurd" + e);
		}

		return result;

	}

	@Override
	public ArrayList<Message> getByAccount(int AccountID) {
		logger.debug("entering getByAccount");
		ArrayList<Message> messageList = new ArrayList<Message>();

		Message message = new Message();

		String query = "Select messageID,accountID,Subject,Text,receiverID FROM Message WHERE accountID = ?";

		try {
			Connection con = openConnection();
			PreparedStatement prepStat = con.prepareStatement(query);
			prepStat.setInt(1, AccountID);
			ResultSet rs = prepStat.executeQuery();
			// Wir erwarten nur ein Ergebnis
			while (rs.next()) {
				message = this.messageRowMapper.mapRow(rs);
				messageList.add(message);
			}
			rs.close();
			prepStat.close();
			con.close();
		} catch (SQLException e) {
			logger.error("SQLException occurd" + e);
		}

		return messageList;
	}

	@Override
	public boolean createMessage(Message message) {
		logger.debug("entering create message");

		boolean result = false;

		String query = "INSERT INTO Message (messageID, accountID, recieverID, subject, text) VALUES "
				+ "(?,?,?,?,?)";

		try {
			Connection con = openConnection();
			PreparedStatement prepStat = con.prepareStatement(query);

			int messageID = getNextID(con, "message");
			logger.debug("new Message has ID=" + messageID);

			prepStat.setInt(1, message.getId());
			prepStat.setInt(2, message.getSender().getID());
			prepStat.setInt(3, message.getReciever().getID());
			prepStat.setString(4, message.getSubject());
			prepStat.setString(5, message.getText());

			prepStat.execute();
			prepStat.close();
			con.close();

			result = true;

		} catch (SQLException e) {
			logger.error("SQLException occured", e);
			result = false;
		}

		return result;

	}

}
